package com.shawn.spring.boot.mybatis.mapper;

import com.shawn.spring.boot.mybatis.entity.Menu;
import com.shawn.spring.boot.mybatis.entity.MenuExample;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface MenuMapper {
  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  long countByExample(MenuExample example);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  int deleteByExample(MenuExample example);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  @Delete({"delete from t_menu", "where menu_id = #{menuId,jdbcType=BIGINT}"})
  int deleteByPrimaryKey(Long menuId);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  @Insert({
    "insert into t_menu (parent_id, name, ",
    "url, type, order_num, ",
    "create_time, update_time, ",
    "is_deleted)",
    "values (#{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
    "#{url,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, #{orderNum,jdbcType=INTEGER}, ",
    "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, ",
    "#{isDeleted,jdbcType=TINYINT})"
  })
  @SelectKey(
      statement = "SELECT LAST_INSERT_ID()",
      keyProperty = "menuId",
      before = false,
      resultType = Long.class)
  int insert(Menu record);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  int insertSelective(Menu record);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  List<Menu> selectByExampleWithRowbounds(MenuExample example, RowBounds rowBounds);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  List<Menu> selectByExample(MenuExample example);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  @Select({
    "select",
    "menu_id, parent_id, name, url, type, order_num, create_time, update_time, is_deleted",
    "from t_menu",
    "where menu_id = #{menuId,jdbcType=BIGINT}"
  })
  @ResultMap("com.shawn.spring.boot.mybatis.mapper.MenuMapper.BaseResultMap")
  Menu selectByPrimaryKey(Long menuId);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  int updateByExampleSelective(@Param("record") Menu record, @Param("example") MenuExample example);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  int updateByExample(@Param("record") Menu record, @Param("example") MenuExample example);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  int updateByPrimaryKeySelective(Menu record);

  /**
   * This method was generated by MyBatis Generator. This method corresponds to the database table
   * t_menu
   *
   * @mbg.generated Sun Aug 30 18:19:25 CST 2020
   */
  @Update({
    "update t_menu",
    "set parent_id = #{parentId,jdbcType=BIGINT},",
    "name = #{name,jdbcType=VARCHAR},",
    "url = #{url,jdbcType=VARCHAR},",
    "type = #{type,jdbcType=INTEGER},",
    "order_num = #{orderNum,jdbcType=INTEGER},",
    "create_time = #{createTime,jdbcType=TIMESTAMP},",
    "update_time = #{updateTime,jdbcType=TIMESTAMP},",
    "is_deleted = #{isDeleted,jdbcType=TINYINT}",
    "where menu_id = #{menuId,jdbcType=BIGINT}"
  })
  int updateByPrimaryKey(Menu record);

  @SelectProvider(type = MenuMapperProvider.class, method = "selectByRoleIdSql")
  @ResultMap("com.shawn.spring.boot.mybatis.mapper.MenuMapper.BaseResultMap")
  List<Menu> selectByRoleId(@Param("roleId") long roleId);

  class MenuMapperProvider {
    public String selectByRoleIdSql(long roleId) {
      StringBuilder builder = new StringBuilder();
      builder
          .append(" select m.* from t_menu m left join t_role_menu rm  ")
          .append(" on m.menu_id = rm.menu_id ")
          .append(" where rm.role_id = #{roleId}");
      return builder.toString();
    }
  }
}
